package com.zy.dp.adapter.example2;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.List;

public class LogFileOperate implements LogFileOperateApi
{

	/**
	 * 
	 */
	
	private String logFilePathName = "Adapter.log";

	public LogFileOperate(String logFilePathName)
	{
		if (logFilePathName != null && logFilePathName.trim().length() > 0)
		{
			this.logFilePathName = logFilePathName;
		}

	}

	@SuppressWarnings("unchecked")
	@Override
	public List<LogModel> readLogFile()
	{

		List<LogModel> list = null;
		ObjectInputStream ois = null;
		try
		{

			File file = new File(logFilePathName);
			if (file.exists())
			{
				ois = new ObjectInputStream(new BufferedInputStream(
						new FileInputStream(file)));

			}

			list = (List<LogModel>) ois.readObject();

		}
		catch (Exception e)
		{
			e.printStackTrace();
		}

		finally
		{

			try
			{
				if (ois != null)
				{
					ois.close();
				}
			}
			catch (IOException e)
			{
				e.printStackTrace();
			}

		}

		return list;

	}

	@Override
	public void writeLogFile(List<LogModel> list)
	{

		File f = new File(logFilePathName);
		ObjectOutputStream oos = null;
		try
		{

			oos = new ObjectOutputStream(new BufferedOutputStream(
					new FileOutputStream(f)));

			oos.writeObject(list);

		}

		catch (IOException e)
		{
			e.printStackTrace();
		}

		finally
		{

			try
			{
				oos.close();

			}
			catch (Exception ex)
			{
				ex.printStackTrace();
			}

		}

	}

}
